import 'package:flutter/material.dart';
import '../../common/api/site_api.dart';
import '../../common/util/common_util.dart';
import '../../widget/card_container.dart';
import '../../widget/empty.dart';
import '../../widget/form_item.dart';
import '../../widget/img_grid_list.dart';

class SiteDetailPage extends StatefulWidget {
  final Map arguments;

  const SiteDetailPage({Key? key, required this.arguments}) : super(key: key);

  @override
  State<SiteDetailPage> createState() => SiteDetailPageState();
}

class SiteDetailPageState extends State<SiteDetailPage> {
  Map? data;

  @override
  void initState() {
    super.initState();
    initData();
  }

  initData() {
    SiteApi.getLiveLoc({"id": widget.arguments['id']}).then((res) {
      if (null != res.data && res.data["isSuccess"]) {
        data = res.data["data"];
        setState(() {});
      } else {
        CommonUtil.showToast(res.data["msg"]);
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  siteDetailBodyBuilder(context) {
    if (null != data) {
      return Column(children: [
        FormItem(
          label: '基地名称',
          value: CommonUtil.valueEmptyToDefStr(data!['name']),
        ),
        FormItem(
          label: '联系电话',
          value: CommonUtil.valueEmptyToDefStr(data!['contactTel']),
        ),
        LayoutBuilder(builder: (buildContext, constraints) {
          return FormItem(
            label: '地址',
            valueWidth: constraints.maxWidth - 75,
            value: CommonUtil.valueEmptyToDefStr(data!['fullAddressInfo']),
          );
        }),
        LayoutBuilder(builder: (buildContext, constraints) {
          return FormItem(
            label: '描述',
            valueWidth: constraints.maxWidth - 50,
            value: CommonUtil.valueEmptyToDefStr(data!['des']),
          );
        }),
        FormItem(
            label: '门面图片',
            value: (data!["locPicList"] ?? []).isNotEmpty ? '' : '-'),
        (data!["locPicList"] ?? []).isNotEmpty
            ? ImgGridList(
                imgList: ((data!['locPicList'] ?? []) as List<dynamic>).cast<String>(),
              )
            : Container(),
      ]);
    } else {
      return const Padding(
        padding: EdgeInsets.only(top: 10.0),
        child: Empty(),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("基地详情"),
        actions: [
          Padding(
            padding: const EdgeInsets.only(right: 2.0),
            child: IconButton(
              icon: const Icon(
                Icons.edit_note_sharp,
                size: 25,
              ),
              onPressed: () {
                Navigator.of(context)
                    .pushNamed("/siteAdd", arguments: widget.arguments);
              },
            ),
          ),
        ],
      ),
      body: SingleChildScrollView(
          controller: ScrollController(),
          child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: CardContainer(
                  margin: const EdgeInsets.only(bottom: 10.0),
                  bodyBuilder: (context) => siteDetailBodyBuilder(context)))),
    );
  }
}
